package com.blrs.service.questionnaire.impl;

import java.util.*;

import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.core.domain.entity.SysUser;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.mapper.*;
import com.blrs.service.questionnaire.IQuestionnaireEnrollService;
import com.blrs.system.mapper.SysUserMapper;
import com.blrs.system.service.ISysDeptService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.domain.QuestionnaireEnroll;
import org.springframework.transaction.annotation.Transactional;

/**
 * 问卷调查作答Service业务层处理
 * 
 * @author yang
 * @date 2023-12-01
 */
@Service
public class QuestionnaireEnrollServiceImpl implements IQuestionnaireEnrollService
{
    @Autowired
    private QuestionnaireEnrollMapper questionnaireEnrollMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private QuestionnaireInvestigationMapper questionnaireInvestigationMapper;
    @Autowired
    private QuestionnaireAnswersMapper questionnaireAnswersMapper;
    @Autowired
    private QuestionnaireReceptionMapper questionnaireReceptionMapper;
    @Autowired
    private QuestionnaireTitleMapper questionnaireTitleMapper;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询问卷调查作答
     * 
     * @param answerId 问卷调查作答主键
     * @return 问卷调查作答
     */
    @Override
    public QuestionnaireEnroll selectQuestionnaireEnrollByAnswerId(String answerId)
    {
        return questionnaireEnrollMapper.selectQuestionnaireEnrollByAnswerId(answerId);
    }

    /**
     * 查询问卷调查作答列表
     * 
     * @param questionnaireEnroll 问卷调查作答
     * @return 问卷调查作答
     */
    @Override
    public List<QuestionnaireEnroll> selectQuestionnaireEnrollList(QuestionnaireEnroll questionnaireEnroll)
    {
        return questionnaireEnrollMapper.selectQuestionnaireEnrollList(questionnaireEnroll);
    }

    /**
     * 新增问卷调查作答
     * 
     * @param questionnaireEnroll 问卷调查作答
     * @return 结果
     */
    @Override
    public int insertQuestionnaireEnroll(QuestionnaireEnroll questionnaireEnroll)
    {
        return questionnaireEnrollMapper.insertQuestionnaireEnroll(questionnaireEnroll);
    }

    /**
     * 修改问卷调查作答
     * 
     * @param questionnaireEnroll 问卷调查作答
     * @return 结果
     */
    @Override
    public int updateQuestionnaireEnroll(QuestionnaireEnroll questionnaireEnroll)
    {
        return questionnaireEnrollMapper.updateQuestionnaireEnroll(questionnaireEnroll);
    }

    /**
     * 批量删除问卷调查作答
     * 
     * @param answerIds 需要删除的问卷调查作答主键
     * @return 结果
     */
    @Override
    public int deleteQuestionnaireEnrollByAnswerIds(String[] answerIds)
    {
        return questionnaireEnrollMapper.deleteQuestionnaireEnrollByAnswerIds(answerIds);
    }

    /**
     * 删除问卷调查作答信息
     * 
     * @param answerId 问卷调查作答主键
     * @return 结果
     */
    @Override
    public int deleteQuestionnaireEnrollByAnswerId(String answerId)
    {
        return questionnaireEnrollMapper.deleteQuestionnaireEnrollByAnswerId(answerId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult insertQuestionnaireEnrollByAnswer(List<List<Integer>> result, String questionnaireId) throws JsonProcessingException {
        QuestionnaireEnroll questionnaireEnroll = new QuestionnaireEnroll();
        questionnaireEnroll.setQuestionnaireId(questionnaireId);
        questionnaireEnroll.setPersonnelId(SecurityUtils.getUserId());
        questionnaireEnroll.setPersonnelName(SecurityUtils.getUsername());
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        questionnaireEnroll.setPersonnelDuties(sysUser.getPosition());
        questionnaireEnroll.setPersonnelContact(sysUser.getPhonenumber());
        questionnaireEnroll.setAnswerTime(new Date());
        questionnaireEnroll.setAnswerEnterpriseId(String.valueOf(SecurityUtils.getOrgId()));
        questionnaireEnroll.setQuestionnaireName(questionnaireInvestigationMapper.selectQuestionnaireInvestigationByQuestionnaireId(questionnaireId).getQuestionnaireName());
        //定义一个分数
        Long score = 0L;
        for (List<Integer> innerList : result) {
            for (Integer integer : innerList) {
                if (Objects.nonNull(integer)){
                    Long score1 = questionnaireAnswersMapper.selectQuestionnaireAnswersBySolutionId(Long.valueOf(String.valueOf(integer))).getScore();
                    score += score1;
                }
            }
        }
        questionnaireEnroll.setQuestionnaireScore(score);
        List<List<Object>> transformedList = new ArrayList<>();
        for (List<Integer> innerList : result) {
            List<Object> transformedInnerList = new ArrayList<>();
            for (Integer value : innerList) {
                // 在这里进行转换，如果当前这个答案对应的题目是多选题，那么这个值就转成boolean存储，方便查看
               if ( questionnaireTitleMapper.selectIsDuoXuan(value)>0){
                   transformedInnerList.add(true);
               }else {
                   transformedInnerList.add(value);
               }
            }
            transformedList.add(transformedInnerList);
        }
        // 使用 Jackson ObjectMapper 将 List<List<Integer>> 转换为 JSON 字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonResult = objectMapper.writeValueAsString(transformedList);
        //答题内容
        questionnaireEnroll.setContent(jsonResult);
        questionnaireEnroll.setAnswerId(UUID.randomUUID().toString().replaceAll("-",""));
        //新增接收方答题数据
        questionnaireEnrollMapper.insertQuestionnaireEnroll(questionnaireEnroll);
        //把接收方是否答题置为一
        questionnaireReceptionMapper.updateQuestionnaireReceptionByReceiverId(SecurityUtils.getOrgId(),questionnaireId);
        return AjaxResult.success("答题完成");
    }

    @Override
    public List<QuestionnaireEnroll> selectIsAnswerListByQuestionnaireId(QuestionnaireEnroll questionnaireEnroll) {
       return questionnaireEnrollMapper.selectEnrollByQuestionnaireIdOnly(questionnaireEnroll.getQuestionnaireId());
    }


}
